//
//  NearByViewController.m
//  TodatPlay
//
//  Created by dllo on 16/9/12.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "NearByViewController.h"
#import "NearByCollectionViewCell.h"
#import "NearByData.h"
#import "NearByRootClass.h"
#import "CDMNetTool.h"
#import "SelfSizeTool.h"
#import "TAMacro.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
#import "MJRefresh.h"
#import "DetailsViewController.h"
#define kCollectionViewCellReuse @"resue"
@interface NearByViewController ()<UICollectionViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *nearByCollectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *nearByLayout;
@property (nonatomic, strong) NearByRootClass *rootClass;
@end

@implementation NearByViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.nearByCollectionView registerNib:[UINib nibWithNibName:@"NearByCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kCollectionViewCellReuse];
    self.nearByLayout.minimumLineSpacing = 10;
    self.nearByLayout.minimumInteritemSpacing = 5;
    self.nearByLayout.sectionInset = UIEdgeInsetsMake(10, 5, 10, 5);
    [self handleData];
    self.nearByCollectionView.bounces = NO;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 180, 44)];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:15];
    label.text = @"附近";
    self.navigationItem.titleView = label;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"left_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]  style:UIBarButtonItemStylePlain target:self action:@selector(handleLeft:)];

}
#pragma mark - 实现左按钮的点击方法
- (void)handleLeft:(UIBarButtonItem *)barBtn {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)handleData {
    NSString *URLString = @"http://wanapi.damai.cn/near/nearList.json?platform=4&cateid=0&source=10345&psize=20&version=203004&d=8&channel_from=m360_market&pindex=0&latlng=38.883461,121.544477";
    [CDMNetTool GET:URLString andBody:nil andHeader:nil andResponse:CDMJSON andSuccessBlock:^(id result) {
        self.rootClass = [NearByRootClass modelWithDic:result];
        [self.nearByCollectionView reloadData];
//        NSLog(@"请求成功:%@", result);
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败%@", error);
    }];
    

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.rootClass.data.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NearByCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellReuse forIndexPath:indexPath];
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:self.rootClass.data[indexPath.row].pic] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
    cell.titleLabel.text = self.rootClass.data[indexPath.row].title;
    cell.venuenameLabel.text = self.rootClass.data[indexPath.row].venuename;
    cell.timestrLabel.text = [NSString stringWithFormat:@"%@·", self.rootClass.data[indexPath.row].timestr];
    cell.viewCountLabel.text = [NSString stringWithFormat:@"%ld", self.rootClass.data[indexPath.row].viewcount];
    cell.pricestrLabel.text = self.rootClass.data[indexPath.row].pricestr;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = [SelfSizeTool selfSizeHeightWithString:self.rootClass.data[indexPath.row].title width:SCREEN_WIDTH font:[UIFont systemFontOfSize:15]];
    return CGSizeMake(SCREEN_WIDTH - 10, 300 + height + 0.5);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DetailsViewController *detailsVC = [[DetailsViewController alloc] init];
    UINavigationController *detaNavi = [[UINavigationController alloc] initWithRootViewController:detailsVC];
    detailsVC.parm = self.rootClass.data[indexPath.row].ID;
    detaNavi.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

    [self presentViewController:detaNavi animated:YES completion:nil];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
